package com.goldmantis.alitrip.controller.system;

import com.goldmantis.alitrip.common.ReturnMessage;
import com.goldmantis.alitrip.common.SysTreeModel;
import com.goldmantis.alitrip.common.annotation.Log;
import com.goldmantis.alitrip.common.sysenum.EnumLogCatagory;
import com.goldmantis.alitrip.common.sysenum.EnumOperationType;
import com.goldmantis.alitrip.pojo.SysRes;
import com.goldmantis.alitrip.service.SysResService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;

/**
 * @Author:Shmily
 * @Description:
 * @Date:Create in 12:31 2018/2/11
 */
@RestController
@Api(description = "资源管理")
@RequestMapping("sys/res")
@RequiresAuthentication

public class SysResController {

    @Autowired
    private SysResService sysResService;

    /**
     * 获取资源
     *
     * @param searchEntity
     * @return
     */
    @GetMapping()
    @ApiOperation(value = "资源列表", httpMethod = "GET")
    public ResponseEntity<ReturnMessage> list(
            @ApiParam("查询对象") SysRes searchEntity){
        try {
            return ResponseEntity.ok(ReturnMessage.success(sysResService.select(searchEntity)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

    @GetMapping(value = "{userId}")
    @ApiOperation(value = "根据Id获取资源", httpMethod = "GET")
    public ResponseEntity<ReturnMessage> getById(
            @ApiParam(value = "主键", required = true) @PathVariable(required = true) Long userId) {
        try {
            return ResponseEntity.ok(ReturnMessage.success(sysResService.selectById(userId)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

    /**
     * 新增资源
     *
     * @param sysRes
     * @return
     */
    @PostMapping()
    @ApiOperation(value = "新增资源", httpMethod = "POST")
    @Log(operationName = "新增资源",logCatagory = EnumLogCatagory.access,operationType = EnumOperationType.add)
    public ResponseEntity<ReturnMessage> post(@ApiParam("资源对象") @RequestBody @Valid SysRes sysRes) {
        try {
            sysResService.insertCustom(sysRes);
            return ResponseEntity.ok(ReturnMessage.success("保存成功！"));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

    /**
     * 编辑资源
     *
     * @param sysRes
     * @return
     */
    @PutMapping()
    @ApiOperation(value = "编辑资源", httpMethod = "PUT")
    public ResponseEntity<ReturnMessage> put(@ApiParam("资源对象") @RequestBody SysRes sysRes) {
        try {
            sysResService.updateCustom(sysRes);
            return ResponseEntity.ok(ReturnMessage.success("更新成功！"));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

    /**
     * 批量删除资源
     *
     * @param res_ids
     * @return
     */
    @DeleteMapping(value = "{res_ids}")
    @ApiOperation(value = "批量删除资源", httpMethod = "DELETE")
    public ResponseEntity<ReturnMessage> delete(@ApiParam("资源id 逗号隔开（1,2）") @PathVariable String res_ids) {
        try {
            List<Object> ids = Arrays.asList(StringUtils.split(res_ids, ","));
            return ResponseEntity.ok(ReturnMessage.success(sysResService.deleteByIds(ids, SysRes.class, "resid")));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

    @GetMapping(value = "getBySystemId/{systemId}")
    @ApiOperation(value = "根据子系统Id获取资源", httpMethod = "GET")
    public ResponseEntity<ReturnMessage> getBySystemId(
            @ApiParam(value = "子系统主键", required = true) @PathVariable(required = true) Long systemId) {
        try {
            return ResponseEntity.ok(ReturnMessage.success(sysResService.getBySystemId(systemId)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

    @GetMapping(value = "getResTree/{systemId}")
    @ApiOperation(value = "获取菜单树json",httpMethod = "GET")
    public ResponseEntity<ReturnMessage> getOrgTree(@PathVariable  Long systemId) {
        try {
            List<SysTreeModel> sysTreeModel = sysResService.getSysTreeModel(systemId);
            return ResponseEntity.ok(ReturnMessage.success(sysTreeModel));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }


    @GetMapping(value = "getResTreeAll")
    @ApiOperation(value = "获取菜单树json",httpMethod = "GET")
    public ResponseEntity<ReturnMessage> getOrgTreeAll() {
        try {
            List<SysTreeModel> sysTreeModel = sysResService.getSysTreeAllModel();
            return ResponseEntity.ok(ReturnMessage.success(sysTreeModel));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }
}
